﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using BackupConsoleClient.BackupServiceReference;
using BackupConsoleClient.TagService;
using Credentials = BackupConsoleClient.BackupServiceReference.Credentials;

namespace BackupConsoleClient
{
    /// <summary>
    /// The actions that can be executed based on the given command type.
    /// </summary>
    public static class BackupEntityCommandActions
    {
        /// <summary>
        /// The instance of the client service.
        /// </summary>
        private static readonly BackupWebServiceClient BackupWebServiceClient;

        

        static BackupEntityCommandActions()
        {
            BackupWebServiceClient = new BackupWebServiceClient();        
        }

        /// <summary>
        /// Load the backup entity based on the id.
        /// </summary>
        /// <returns></returns>
        public static BackupEntity LoadById()
        {
            //Load the file from backup system based on the id.
            var backupEntityById = BackupWebServiceClient.LoadById(
                Guid.Parse(CommandLineArguments.Instance.Name),
                !string.IsNullOrWhiteSpace(CommandLineArguments.Instance.UploadedBy)
                    ? CommandLineArguments.Instance.UploadedBy
                    : CommandLineArguments.Instance.User,
                new Credentials()
                {
                    User = CommandLineArguments.Instance.User,
                    Password = CommandLineArguments.Instance.Password
                });

            return backupEntityById;
        }

        /// <summary>
        /// Load the backup entity based on the name.
        /// </summary>
        /// <returns></returns>
        public static BackupEntity LoadByName()
        {
            var backupEntity = BackupWebServiceClient.LoadByName(
                CommandLineArguments.Instance.Name,
                !string.IsNullOrWhiteSpace(CommandLineArguments.Instance.UploadedBy)
                    ? CommandLineArguments.Instance.UploadedBy
                    : CommandLineArguments.Instance.User,
                       new Credentials()
                       {
                           User = CommandLineArguments.Instance.User,
                           Password = CommandLineArguments.Instance.Password
                       });

            return backupEntity;
        }

        /// <summary>
        /// Save the file to backup system.
        /// </summary>
        public static void Save()
        {
            BackupWebServiceClient.Save(new BackupEntity()
            {
                Description = CommandLineArguments.Instance.Description,
                Name = Path.GetFileName(CommandLineArguments.Instance.File),
                OriginalFileName = CommandLineArguments.Instance.OriginalFileName,
                Contents = File.ReadAllBytes(CommandLineArguments.Instance.File),
                UploadBy = CommandLineArguments.Instance.User
            },
                                                    new Credentials()
                                                    {
                                                        User = CommandLineArguments.Instance.User,
                                                        Password = CommandLineArguments.Instance.Password
                                                    });
        }

        /// <summary>
        /// Get the backup stream of a specific file from backup system.
        /// </summary>
        public static Stream GetBackupStream()
        {
            var stream = BackupWebServiceClient.GetBackupStream(
                Guid.Parse(CommandLineArguments.Instance.Name),
                 !string.IsNullOrWhiteSpace(CommandLineArguments.Instance.UploadedBy)
                    ? CommandLineArguments.Instance.UploadedBy
                    : CommandLineArguments.Instance.User,
                     new Credentials()
                     {
                         User = CommandLineArguments.Instance.User,
                         Password = CommandLineArguments.Instance.Password
                     });

            return stream;
        }

        /// <summary>
        /// Load the file localy. The file is saved on a specific path.
        /// </summary>
        public static void LoadContentToFile()
        {
            using (var streamRead = BackupEntityCommandActions.GetBackupStream())
            {
                //Save the stream to the give location.
                if (File.Exists(CommandLineArguments.Instance.File))
                {
                    File.Delete(CommandLineArguments.Instance.File);
                }
                using (var streamWrite = File.Create(CommandLineArguments.Instance.File))
                {
                    byte[] buffer = new byte[1024];
                    int read;

                    while ((read = streamRead.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        streamWrite.Write(buffer, 0, read);
                    }
                }
            }
        }

        /// <summary>
        /// Load all files information for a specific user.
        /// </summary>
        /// <returns>The list of file information.</returns>
        public static BackupEntity[] LoadAll()
        {
            return BackupWebServiceClient.LoadAll(
                !string.IsNullOrWhiteSpace(CommandLineArguments.Instance.UploadedBy)
                    ? CommandLineArguments.Instance.UploadedBy
                    : CommandLineArguments.Instance.User,
                new Credentials()
                    {
                        User = CommandLineArguments.Instance.User,
                        Password = CommandLineArguments.Instance.Password
                    });
        }

        /// <summary>
        /// Delete a specific file.
        /// </summary>
        public static void Delete()
        {
            BackupWebServiceClient.Delete(
                Guid.Parse(CommandLineArguments.Instance.Name),
                 !string.IsNullOrWhiteSpace(CommandLineArguments.Instance.UploadedBy)
                    ? CommandLineArguments.Instance.UploadedBy
                    : CommandLineArguments.Instance.User,
                     new Credentials()
                     {
                         User = CommandLineArguments.Instance.User,
                         Password = CommandLineArguments.Instance.Password
                     });
        }

       

        /// <summary>
        /// Get all entities with the given tag
        /// </summary>
        /// <returns>The list of entities with the given id.</returns>
        public static ReadOnlyCollection<BackupEntity> GetBackupEntitiesByTag()
        {
            return BackupWebServiceClient.GetBackupEntitiesByTag(
                Guid.Parse(CommandLineArguments.Instance.Id),
                new Credentials()).ToList().AsReadOnly();
        }
    }
}
